<template>
  <div>
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <el-button @click="btnClick">切换语言</el-button>
    <h3>{{ $t('xxx') }}</h3>
    <el-calendar v-model="value" v-if="bol"> </el-calendar>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
// 创建实例对象
const cos = new COS({
  // 密钥
  SecretId: 'AKIDQebHgqkNjpeCTTEivrWu1sRloa5KIyNW',
  SecretKey: 'tWrvjHYYHjvqmSs1tWsxlJaaIq89tMZW'
})
export default {
  data () {
    return {
      imageUrl: '',
      value:'',
      bol:true
    }
  },
  methods: {
    // 上传前的回调  上传限制
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    httpRequest (data) {
      console.log(data)
      cos.putObject(
        {
          Bucket: '75-1308138770' /* 存储桶 */,
          Region: 'ap-guangzhou' /* 存储桶所在地域，必须字段 */,
          Key: toString(data.file.uid) /* 标识 */,
          StorageClass: 'STANDARD', // 固定值
          Body: data.file, // 上传文件对象  file文件对象
          onProgress: progressData => {
            // 上传进度，实时返回上传文件进度
            console.log(JSON.stringify(progressData))
          }
          // 用箭头函数  使得this指向为vue实例  因为vue的实例this只在method方法第一层生效
        },
        (err, data) => {
          // 上传成功或者失败处理
          // err为空就是成功，err有值就是失败了
          // data就是上传成功后腾讯云返回的数据（上传文件地址）
          this.imageUrl = 'http://' + data.Location
          console.log(err || data)
        }
      )
    },
    btnClick () {
      this.$i18n.locale = this.$i18n.locale === 'zh' ? 'en' : 'zh'
      this.bol=false
      this.$nextTick(()=>{
        this.bol = true
      })
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
